#include "cmd_handle.h"
#include "cmd_cp.h"
#include "cmd_ls.h" 

int cmd_exec(char * command){
	command_t* cmd=init_command();
	if(NULL == cmd)return -1;
	parse_command(command,cmd);
	return dispatch_command(cmd);
}
int dispatch_command(command_t* command){
	char * cmd_name=command->cmd_name;
	if(strcmp(cmd_name,"cp")==0){
		return operate_cp(command);
	}else if(strcmp(cmd_name,"ls")==0){
		return operate_ls(command);
	}else{
		return -1;
	}
}
void parse_command(char* command,command_t* cmd){
	char* tokenPtr=strtok(command," ");
	strcpy(cmd->cmd_name,tokenPtr);
	while((tokenPtr=strtok(NULL," "))!=NULL){
		strcpy(cmd->cmd_params[cmd->cmd_argc],tokenPtr);
		cmd->cmd_argc+=1;
	}
}
command_t* init_command(){
	return calloc(1,sizeof(command_t));
}
